package com.atguigu.flink.utils;

import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Smexy on 2023/1/30
 */
public class MyUtil
{
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /*
            把ts格式化为时间

            LocalDateTime:  代表一个时间对象
            DateTimeFormatter: 时间格式
     */
    public static String parseTime(TimeWindow timeWindow){

        LocalDateTime startTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeWindow.getStart()), ZoneId.of("Asia/Shanghai"));
        LocalDateTime endTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeWindow.getEnd()), ZoneId.of("Asia/Shanghai"));

        return "[" + startTime.format(formatter) + "," + endTime.format(formatter) + ")";

    }

    //把可迭代的集合封装为List
    public static <T> List<T> parseList(Iterable<T> elements){

        List<T> result = new ArrayList<>();

        for (T element : elements) {
            result.add(element);
        }

        return result;

    }
}
